package com.nsoft.ui;

import com.nsoft.objects.BookItem;
import com.nsoft.ui.R;
import com.nsoft.utils.Constant;
import com.nsoft.utils.SimpleGestureFilter;
import com.nsoft.utils.SimpleGestureFilter.SimpleGestureListener;
import com.nsoft.utils.engine;
import com.nsoft.utils.utils;
import com.nsoft.utils.viewengine;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ViewFlipper;
import android.app.Activity;
import android.content.Intent;

public class ViewActivity extends Activity 
			implements SimpleGestureListener {
	
    Animation animFlipInForeward;
    Animation animFlipOutForeward;
    Animation animFlipInBackward;
    Animation animFlipOutBackward;
	
	private SimpleGestureFilter detector;
	private ViewFlipper pages;
	private String bookPath;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.book_detail_view);
        detector = new SimpleGestureFilter(this,this);
        
        animFlipInForeward = AnimationUtils.
        		loadAnimation(this,R.anim.flipin);
        animFlipOutForeward = AnimationUtils.
        		loadAnimation(this,R.anim.flipout);
        animFlipInBackward = AnimationUtils.
        		loadAnimation(this,R.anim.flipin_reverse);
        animFlipOutBackward = AnimationUtils.
        		loadAnimation(this,R.anim.flipout_reverse);

        
        pages=(ViewFlipper)findViewById(
        			R.id.mainViewFlipper);
        pages.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(
					View v, MotionEvent event) {
				detector.onTouchEvent(event);
				return true;
			}
		});
        
        ImageButton homeBtn=(ImageButton)
        		findViewById(R.id.homeButton);
        homeBtn.setOnClickListener(new View.
        		OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				setResult(RESULT_OK,intent);
				finish();
			}
		});
        
        startUp();
    }

    private void startUp(){
    	Intent intent=getIntent();
    	int pos=intent.getExtras().getInt(
    		Constant.booklist.BOOK_INDEX_TRANSFER);
    	BookItem book=engine.getBookItem(pos);
    	bookPath=utils.getBookPath()+Constant.PATH_SPLITTER+
    			/* book.getContentId()+Constant.PATH_SPLITTER+*/
    			book.getContentName();
    	
    	String bookConfigPath=bookPath+Constant.PATH_SPLITTER+
    			Constant.xml.XML_BOOK_CONFIG_FILE;
    	
    	pages.removeAllViews();
    	viewengine.getBookFromBookConfig(bookConfigPath);
    	
    	View[] viewList=viewengine.createViews(this);
    	for (int i=0;i<viewList.length;i++){
    		pages.addView(viewList[i]);
    	}

    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
    	this.detector.onTouchEvent(ev);
    	return super.dispatchTouchEvent(ev);
    }
    
    @Override
    public void onSwipe(int direction) {

    	switch (direction) {
    		case SimpleGestureFilter.SWIPE_RIGHT : {
    	    	pages.setInAnimation(animFlipInBackward);
    			pages.setOutAnimation(animFlipOutBackward);
    			pages.showPrevious();
    	        break;
    		}
    		case SimpleGestureFilter.SWIPE_LEFT : {
    	    	pages.setInAnimation(animFlipInForeward);
    			pages.setOutAnimation(animFlipOutForeward);
    			pages.showNext();
    	        break;
    		}
    		case SimpleGestureFilter.SWIPE_DOWN : {
    	        break;
    		}
    		case SimpleGestureFilter.SWIPE_UP : {
    			break;
    		}
    	}
    }
    
    @Override
    public void onDoubleTap() {
    	// to do here
    }
}
